<template lang="">
  <div>
    <el-table style="width: 100%;margin-top:10px" border :data="records">
      <el-table-column type="index" label="序号" align="center" width="80px" />
      <el-table-column label="名称" prop="skuName" align="center" width="width" />
      <el-table-column label="描述" prop="skuDesc" align="center" width="width" />
      <el-table-column label="默认图片" align="center" width="150px">
        <template slot-scope="{row, $index}">
          <div>
            <img :src="row.skuDefaultImg" style="width:100px;height:100px">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="重量(KG)" prop="weight" align="center" width="150px" />
      <el-table-column label="价格(元)" prop="price" align="center" width="150px" />
      <el-table-column label="操作" prop="" width="width" align="center">
        <template slot-scope="{row, $index}">
          <div>
            <!-- 这里的按钮将来用hintButton替换 -->
            <el-button v-if="row.isSale==0" type="success" icon="el-icon-bottom" size="mini" @click="sale(row)" />
            <el-button v-else type="info" icon="el-icon-top" size="mini" @click="cancelSale(row)" />
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(row)" />
            <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)" />
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteSpu(row)" />
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 当前第几页、数据总条数、每一页展示条数、连续页码 -->
    <el-pagination
      style="margin-top:20px;text-align:center"
      background
      :current-page="page"
      :total="total"
      :page-size="limit"
      :pager-count="7"
      :page-sizes="[3, 5, 10]"
      layout="prev, pager, next,jumper, ->, sizes, total"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    />
    <!-- 抽屉效果 -->
    <el-drawer
      :visible.sync="drawer"
      :direction="direction"
      :show-close="false"
      size="50%"
    >
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <div>
              <el-tag v-for="(attr, inndex) in skuInfo.skuAttrValueList" :key="attr.id" type="success" style="margin:0 10px 5px 0">{{attr.attrId}}-{{attr.valueId}}</el-tag>
            </div>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="500px" trigger="click">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id" style="width:570px;height:500px;">
              <!-- <h3 class="small">{{ item }}</h3> -->
              <img :src="item.imgUrl" style="width:570px;height:500px;display:block;margin:auto">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>
<script>
export default {
  name: 'Sku',
  data() {
    return {
      //
      records: [],
      page: 1,
      limit: 3,
      total: 0,
      // 存储SKU信息
      skuInfo: {},
      drawer: false,
      // Drawer 打开的方向 rtl / ltr / ttb / btt
      direction: 'rtl'
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    async getSkuList(pager = 1) {
      this.page = pager
      const { page, limit } = this
      const result = await this.$API.sku.reqSkuList(page, limit)
      console.log('er', result)
      if (result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    //
    handleSizeChange(limit) {
      // 当PageSize改变时会触发
      this.limit = limit
      this.getSkuList()
    },
    // 上架
    async sale(row) {
      const result = await this.$API.sku.reqOnSale(row.id)
      console.log('上架', result);
      if (result.code == 200) {
        row.isSale == 1
        this.$message.success('上架成功！')
        this.getSkuList()
      }
    },
    // 下架 reqCancelSale
    async cancelSale(row) {
      const result = await this.$API.sku.reqCancelSale(row.id)
      console.log('下架', result);
      if (result.code == 200) {
        row.isSale == 0
        this.$message.warning('下架成功！')
        this.getSkuList()
      }
    },
    //
    edit() {
      this.$message.info('正在开发中~~~')
    },
    // 获取SKU详情的方法
    async getSkuInfo(sku) {
      // 打开抽屉
      this.drawer = true
      const result = await this.$API.sku.reqSkuById(sku.id)
      console.log('SKU详情', result)
      if (result.code == 200) {
        this.skuInfo = result.data
      }
    }
    // // 关闭前的回调，会暂停 Drawer 的关闭
    // handleClose(done) {
    //   this.$confirm('确认关闭？')
    //     .then(_ => {
    //       done()
    //     })
    //     .catch(_ => {})
    // }
  }
}
</script>
<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }
  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
</style>

<style scoped>
  .el-row .el-col-5 {
    /* margin: 0 20px; */
    font-size: 18px;
    text-align: right;
  }
  .el-col {
    margin: 10px 10px;
  }
  >>>.el-carousel__button {
    width: 10px;
    height: 10px;
    background: red;
    border-radius: 50%;
  }
</style>
